package com.yc.web.servlets.admin;

import com.yc.model.JsonModel;
import com.yc.web.servlets.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author zwei
 * @create 2024-07-02 20:03
 */
@WebServlet("/admin/permission.action")
public class PermissionServlet extends BaseServlet {
    public void permission(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
        String year = req.getParameter("year");
        String sql = "\n" +
                "SELECT MONTH(order_date) AS month, SUM(total_price) AS sales\n" +
                "FROM tb_order\n" +
                "WHERE order_status = 1\n" +
                "  AND YEAR(order_date) = ?\n" +
                "GROUP BY MONTH(order_date)\n" +
                "ORDER BY month;\n";
        List<Map<String, Object>> list = dbHelper.select(sql, year);
        System.out.println(list);
        JsonModel jm = new JsonModel();
        jm.setCode(1);
        jm.setObj(list);
        super.writeJson(jm, resp);
    }

    public void foodPermissoon(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sql="select totalbuynum , food_name from tb_food order by totalbuynum desc";
        List<Map<String,Object>>list=dbHelper.select(sql);
        JsonModel jm=new JsonModel();
        if (list.size()>=5){
            List<Map<String,Object>>list1=list.subList(0,5);
            System.out.println(list1);
            jm.setCode(1);
            jm.setObj(list1);
        }else {
            System.out.println(list);
            jm.setCode(1);
            jm.setObj(list);
        }
        super.writeJson(jm,resp);
    }
}
